# coding:UTF-8
# ________________________
# Project_Name:pyzidonghua
# File_Name:Administrator
# Author:Administrator
# Date_Time:2022/4/24 19:22
# Description:
# ________________________
import unittest  # 导入测试框架

from selenium import webdriver
import time
from selenium.webdriver.common.by import By

from selenium.webdriver.support.wait import WebDriverWait  # 导入显式等待类
from selenium.webdriver.support import expected_conditions  # 导入等待条件

from selenium.webdriver.common.action_chains import ActionChains


class BDtest(unittest.TestCase):  # 必须继承TestCase类
    """
    标准的测试用例分为：测试前，测试中，测试后
    """

    # 测试前：前置条件
    def setUp(self) -> None:
        self.driver = webdriver.Chrome()

        self.wait = WebDriverWait(self.driver, 30) # 生成显式等待对象，设置最大元素等待时间
        self.driver.maximize_window() #最大化浏览器
        self.driver.get("https://map.baidu.com/@11588878,3564790,13z/") #百度地图URL
        self.driver.find_element(By.CLASS_NAME, 'close-btn').click()  #点击进入百度地图

        # 测试中：测试步骤
    def test(self)-> None:
        # self.wait.until(expected_conditions.visibility_of_element_located((By.ID, "sole-input"))) #显式等待
        self.driver.find_element(By.ID,'sole-input').send_keys('圣心幼稚园')  #在输入框输入'圣心幼稚园'
        time.sleep(1)

        # self.wait.until(expected_conditions.visibility_of_element_located((By.ID, "search-button")))
        self.driver.find_element(By.ID,'search-button').click()  #点击搜索
        time.sleep(1)

        self.driver.find_element(By.XPATH,'//*[@id="cards-level1"]/li/div/div[1]/ul/li[1]/div[1]/div[3]/div[1]/span/a')\
            .click()  #点击弹出对话框  '圣心幼稚园'
        time.sleep(1)

        # self.wait.until(expected_conditions.visibility_of_element_located((By.ID, '//*[@id="route-go"]')))
        self.driver.find_element(By.XPATH,'//*[@id="route-go"]').click()  #点击 '到这去'
        time.sleep(3)

        self.wait.until(expected_conditions.visibility_of_element_located
                        ((By.XPATH,'//*[@id="route-searchbox-content"]/div[2]/div/div[2]/div[1]/input')))
        self.driver.find_element(By.XPATH,'//*[@id="route-searchbox-content"]/div[2]/div/div[2]/div[1]/input')\
            .send_keys('祥福苑大厦')  #输入出发地

        # self.wait.until(expected_conditions.visibility_of_element_located((By.ID, 'search-button')))
        self.driver.find_element(By.ID, 'search-button').click()  #点击搜索
        time.sleep(3)

        # self.wait.until(expected_conditions.visibility_of_element_located
        #                 ((By.XPATH,'//*[@id="route-searchbox-content"]/div[1]/div[1]/div[2]')))
        self.driver.find_element(By.XPATH,'//*[@id="route-searchbox-content"]/div[1]/div[1]/div[2]').click()  #点击驾车
        time.sleep(3)

        s = self.driver.find_element(By.XPATH,'//*[@id="platform"]/div[2]/div[2]/div[5]')  #把终点坐标放入变量S
        ActionChains(self.driver).click_and_hold(s).perform()   #点击左键不放
        ActionChains(self.driver).move_by_offset(100, 100).perform()  #拖拽鼠标到指定位置
        ActionChains(self.driver).release().perform()  #松开鼠标
        time.sleep(2)




    # 测试后：清理测试环境
    def tearDown(self) -> None:
        self.driver.quit()
